#coding:utf-8
import json
import re
from django.http import HttpResponse
from django.core.mail import EmailMessage
from django.contrib.auth.decorators import login_required
from lovewith.settings import EMAIL_HOST_USER, EMAIL_HOST_PASSWORD
from lovewith.website.views.modules.province import ProvinceApi



#发送邮件
@login_required
def send_mail(request):
    mail = request.POST.get('mail')
    subject = request.POST.get('subject')
    content = request.POST.get('content')
    if mail and subject and content:
        mail_list = [mail]
        if re.match('^([a-zA-Z0-9_\.\-])+@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$', mail):
            try:
                msg = EmailMessage(subject, content, EMAIL_HOST_USER, mail_list)
                msg.content_subtype = 'html'
                msg.send()
            except StandardError, api_error:
                pass
    return HttpResponse(json.dumps({}))


#反馈信息
def send_feedback(request):
    mail = 'admin@lovewith.me'
    subject = '[网站反馈]'
    content = request.POST.get('content')
    if content:
        mail_list = [mail]
        try:
            msg = EmailMessage(subject, content, EMAIL_HOST_USER, mail_list)
            msg.content_subtype = 'html'
            msg.send()
        except Exception, e:
            print e
    return HttpResponse(json.dumps({}))


#获取省份&&城市列表
def get_city_and_province(request):
    city_data = ProvinceApi.get_all()

    return HttpResponse(json.dumps(city_data))